﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Transformer.iHunter.Plugins.Email
{
    public partial class EmailConfigControl : UserControl , System.Xml.Serialization.IXmlSerializable
    {
        public EmailConfigControl()
        {
            InitializeComponent();
        }

        private bool IsConfigureValid(out string remark)
        {
            remark = "";
            if (comboBox_Protocal.SelectedIndex != 0 && comboBox_Protocal.SelectedIndex != 1)
            {
                remark = "Please select the protocal";
                return false;
            }

            if (textBox_SendMailServer.Text == null || textBox_SendMailServer.Text.Trim() == "")
            {
                remark = "Please input the send emails server";
                return false;
            }
            if (textBox_ReceiveServerPort.Text == null || textBox_ReceiveServerPort.Text.Trim() == "")
            {
                remark = "Please input your send emails serve port";
                return false;
            }
            try
            {
                int port = Convert.ToInt32(textBox_ReceiveServerPort.Text);
                if (!(port > 0))
                {
                    remark = "You must enter a positive number for send server port";
                    return false;
                }
            }
            catch (System.FormatException e)
            {

                remark = "You must enter a number for send server port";
                return false;
            }
            catch (System.OverflowException e)
            {
                remark = "Your number for send server port is too big";
                return false;
            }
            if (textBox_ReceiveMailServer.Text == null || textBox_ReceiveMailServer.Text.Trim() == "")
            {
                remark = "Please input the receive emails server";
                return false;
            }
            

            if (textBox_SendServerPort.Text == null || textBox_SendServerPort.Text.Trim() == "")
            {
                remark = "Please input your receive emails server port";
                return false;
            }
            try
            {
                int port = Convert.ToInt32(textBox_SendServerPort.Text);
                if (!(port > 0))
                {
                    remark = "You must enter a positive number for receive server port";
                    return false;
                }
            }
            catch (System.FormatException e)
            {
                remark = "You must enter a number for receive server port";
                return false;
            }
            catch (System.OverflowException e)
            {
                remark = "Your number for receive server port is too big";
                return false;
            }

            if (textBox_Username.Text == null || textBox_Username.Text.Trim() == "")
            {
                remark = "Please input your username";
                return false;
            }
            if (textBox_Password.Text == null || textBox_Password.Text == string.Empty)
            {
                remark = "Please input your password";
                return false;
            }
            

            
            return true;
        }

        public XmlDocument GetConfigure()
        {
            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("EmailChannelSettings");
            doc.AppendChild(root);

            XmlElement ele = doc.CreateElement("protocal");
            ele.InnerText = comboBox_Protocal.SelectedIndex.ToString();
            root.AppendChild(ele);

            ele = doc.CreateElement("s_server");

            ele.InnerText = textBox_SendMailServer.Text.Trim();
            root.AppendChild(ele);

            ele = doc.CreateElement("s_port");
            ele.InnerText = textBox_SendServerPort.Text.Trim();
            root.AppendChild(ele);

            ele = doc.CreateElement("s_ssl");
            ele.InnerText = checkBox_SendServerSSL.Checked ? "true" : "false";
            root.AppendChild(ele);

            ele = doc.CreateElement("r_server");
            ele.InnerText = textBox_ReceiveMailServer.Text.Trim();
            root.AppendChild(ele);

            ele = doc.CreateElement("r_port");
            ele.InnerText = textBox_ReceiveServerPort.Text.Trim();
            root.AppendChild(ele);

            ele = doc.CreateElement("r_ssl");
            ele.InnerText = checkBox_ReceiveServerSSL.Checked ? "true" : "false";
            root.AppendChild(ele);

            ele = doc.CreateElement("username");
            ele.InnerText = textBox_Username.Text.Trim();
            root.AppendChild(ele);

            ele = doc.CreateElement("password");
            ele.InnerText = textBox_Password.Text;
            root.AppendChild(ele);

            ele = doc.CreateElement("opt_d");
            ele.InnerText = checkBox_DeleteMailsOnServer.Checked ? "true" : "false";
            root.AppendChild(ele);

            return doc;
        }

        public void SetCongifure(XmlNode root)
        {
            try
            {
                XmlElement ele = root["protocal"];
                int index = Convert.ToInt32(ele.InnerText);
                comboBox_Protocal.SelectedIndex = index;

                ele = root["s_server"];
                textBox_SendMailServer.Text = ele.InnerText;

                ele = root["s_port"];
                textBox_SendServerPort.Text = ele.InnerText;

                ele = root["s_ssl"];
                if (ele.InnerText == "true")
                {
                    checkBox_SendServerSSL.Checked = true;
                }
                else{
                    checkBox_SendServerSSL.Checked = false;
                }

                ele = root["r_server"];
                textBox_ReceiveMailServer.Text = ele.InnerText;

                ele = root["r_port"];
                textBox_ReceiveServerPort.Text = ele.InnerText;

                ele = root["r_ssl"];
                if (ele.InnerText == "true")
                {
                    checkBox_ReceiveServerSSL.Checked = true;
                }
                else
                {
                    checkBox_ReceiveServerSSL.Checked = false;
                }

                ele = root["username"];
                textBox_Username.Text = ele.InnerText;

                ele = root["password"];
                textBox_Password.Text = ele.InnerText;

                ele = root["opt_d"];
                if (ele.InnerText == "false")
                {
                    checkBox_DeleteMailsOnServer.Checked = false;
                }
                else{
                    checkBox_DeleteMailsOnServer.Checked =true;
                }
            }
            catch { }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(reader);
            SetCongifure(doc.DocumentElement);
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            string reason;
            if (!IsConfigureValid(out reason))
            {
                MessageBox.Show(reason);

                throw new ArgumentException(reason);
            }

            XmlDocument doc = GetConfigure();
            doc.WriteTo(writer);
        }

        #endregion
    }
}
